// pages/shop/shop.js
Page({
  data: {
    id: wx.getStorageSync('userid'),
    list:[],
    addgood:[],
  },

  onLoad: function (options) {
    this.setData({
      id: wx.getStorageSync('userid')
    })
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/goods',
      header:{
        'content-type':'application/json'
      },
      success : res => {
        // console.log(res.data);
        this.setData({
          list:res.data
        })
      }
    })
    // console.log(this.data)
  },

  addgift:function(e){
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res){
          if(res.confirm){
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home',
            })
          }else if(res.cancel){
            console.log('取消')
          }
        }
      })
    }else{
      console.log(e.currentTarget.dataset.id)
      wx.request({
        url: `https://www.prebeauty.work:3001/api/goods?goodsid=${e.currentTarget.dataset.id}`,
        header: {
          'content-type': 'application/json'
        },
        success:async (res)=>{
          // console.log(res.data)
          this.setData({
            addgood:res.data[0]
          })
          console.log(this.data.id)
          wx.request({
            url: 'https://www.prebeauty.work:3001/api/cart',
            data: {
              goodsid:this.data.addgood.goodsid,
              shopid:this.data.addgood.shopid,
              goodsname:this.data.addgood.goodsname,
              goodsprice:this.data.addgood.goodsprice,
              goodsimg:this.data.addgood.goodsimg,
              goodsbuynum:1,
              userid:this.data.id
            },
            header: {
              'content-type': 'application/json'
            },
            method: 'POST',
            success: (res)=>{
              console.log('add success')
              wx.showToast({
                title: '添加成功！',
                icon: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    }
  },

  fenleiAnimal:function(e){
    console.log(e.currentTarget.dataset)
    wx.request({
      url: `https://www.prebeauty.work:3001/api/goods`,
      header:{
        'content-type':'application/json'
      },
      success : res => {
        // console.log(res.data)
        var i=0;
        var list=[];
        for(i in res.data){
          if(res.data[i].goodstype == e.currentTarget.dataset.animal || res.data[i].goodstype == '通用'){
            list.push(res.data[i]);
            // console.log(list)
          }
        }
        this.setData({
          list:list
        })
      }
    })
    console.log(this.data.list)
  },

  fenleiAll:function(e){
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/goods',
      header:{
        'content-type':'application/json'
      },
      success : res => {
        // console.log(res.data);
        this.setData({
          list:res.data
        })
      }
    })
  },

  fenleiUse:function(e){
    console.log(e.currentTarget.dataset)
    wx.request({
      url: `https://www.prebeauty.work:3001/api/goods`,
      header:{
        'content-type':'application/json'
      },
      success : res => {
        // console.log(res.data)
        var i=0;
        var list=[];
        for(i in res.data){
          if(res.data[i].use === e.currentTarget.dataset.use){
            list.push(res.data[i]);
            // console.log(list)
          }
        }
        this.setData({
          list:list
        })
      }
    })
    console.log(this.data.list)
  },
})